package com.campuscrowdsourcing.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campuscrowdsourcing.server.annotation.Log;
import com.campuscrowdsourcing.server.common.enums.Action;
import com.campuscrowdsourcing.server.domain.entity.TaskCheck;
import com.campuscrowdsourcing.server.domain.entity.TeamCheck;
import com.campuscrowdsourcing.server.domain.model.AjaxResult;
import com.campuscrowdsourcing.server.service.TeamCheckService;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xinyuan Zhao
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/api/teamCheck")
public class TeamCheckController {
    @Resource
    private TeamCheckService teamcheckService;

    @PostMapping
    @Log(title = "新增团队审核", action = Action.INSERT)
    public AjaxResult saveTeamCheck(@RequestBody TeamCheck teamcheck) {
        teamcheckService.save(teamcheck);
        return AjaxResult.success();
    }

    @PutMapping
    @Log(title = "编辑团队审核", action = Action.UPDATE)
    public AjaxResult updateTeamCheck(@RequestBody TeamCheck teamcheck) {
        teamcheckService.updateById(teamcheck);
        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    @Log(title = "删除团队审核", action = Action.DELETE)
    public AjaxResult deleteTeamCheck(@PathVariable Integer id) {
        teamcheckService.removeById(id);
        return AjaxResult.success();
    }

    @PostMapping("/del/batch")
    @Log(title = "批量删除团队审核", action = Action.DELETE)
    public AjaxResult deleteBatchTeamCheck(@RequestBody List<Integer> ids) {
        teamcheckService.removeByIds(ids);
        return AjaxResult.success();
    }

    @GetMapping
    @Log(title = "查看团队审核", action = Action.SELECT)
    public AjaxResult listTeamCheck() {
        return AjaxResult.success(teamcheckService.list());
    }

    @GetMapping("/{id}")
    @Log(title = "id查找团队审核", action = Action.SELECT)
    public AjaxResult getTeamCheckById(@PathVariable Integer id) {
        TeamCheck teamCheck = teamcheckService.getById(id);
        if(teamCheck == null)
        {
            return AjaxResult.fail("该任务审核已通过或不存在");
        }
        return AjaxResult.success(teamCheck);
    }

    @GetMapping("/page")
    @Log(title = "分页查看团队审核", action = Action.SELECT)
    public AjaxResult listTeamCheckPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<TeamCheck> queryWrapper = new QueryWrapper<TeamCheck>().orderByDesc("id");
        queryWrapper.like(!"".equals(name), "name", name);
        return AjaxResult.success(teamcheckService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

